package com.atguigu.springcloud.alibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.springcloud.alibaba.feign_service.PaymentService;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class CircleBreakerFeignController {

    @Resource
    private PaymentService paymentService;


    @GetMapping("/consumer/feign/{id}")
    @SentinelResource(value = "fallback", blockHandler = "blockError")
    public CommonResult<Payment> fallback(@PathVariable Long id) {
//        int a=10/0;
        CommonResult<Payment> result = paymentService.paymentSQL(id);
        //超时  报错
        return result;
    }

    public CommonResult<Payment> blockError(@PathVariable Long id, BlockException e) {
        Payment payment = new Payment(id, null);
        return new CommonResult<>(450, "sentinel限流", payment);
    }
}
